<?php


namespace Help\Controller;

use Think\Controller;

/**
 * Class IndexController
 * @package Help\Controller
 * @author:xjw129xjt xjt@ourstu.com
 */
class IndexController extends Controller
{

    protected $mHelpTypeModel = null;
    protected $mHelpModel = null;
    protected $mHelpAttendModel = null;
    protected $mHelpWorksModel = null;
    protected $mHelpSnapshotModel = null;
    protected $mHelpAttendSupportModel = null;
    protected $mHelpWorksSupportModel = null;
    protected $mHelpEvaluateModel = null;
    public function _initialize()
    {
        $config = api('Config/lists');
        C($config); //添加配置
       $this->mHelpTypeModel =  D('HelpType');
        $this->mHelpModel = D('Help');
        $this->mHelpAttendModel = D('HelpAttend');
        $this->mHelpWorksModel = D('HelpWorks');
        $this->mHelpSnapshotModel = D('HelpSnapshot');
        $this->mHelpAttendSupportModel = D('help_attend_support');
        $this->mHelpWorksSupportModel = D('help_works_support');
        $this->mHelpEvaluateModel =    D('HelpEvaluate');
        $this->setTitle('帮忙');

    }

    public function index()
    {
        $post = I('post.');
        if (empty($post)) {
            $post['sex'] = -1;
            $post['state'] = -1;
            $post['sort'] = -1;
        }
        $map = $this->getMap($post);
        if (array_key_exists('_string', $map) || array_key_exists('number', $map) || array_key_exists('sex', $map) || array_key_exists('sort', $map)|| array_key_exists('state', $map) || $post['sort'] != -1 ) {
            $this->assign('flag', true);
        }
        if($post['sort'] == 1){
            $order = 'sign_count desc';
        }elseif($post['sort'] == 2){
            $order = 'user_rate desc';
        }else{
            $order = 'recommend desc';
        }

        $this->assign('filter_height', array(50,100,130,155, 160, 165, 170, 175, 180, 185, 190, 195, 200));
        $this->assign('filter_number', array(5, 10, 20, 50, 100));
        $this->assign('filter_age', array(3,6,10,16,18, 20, 24, 30, 40, 60));
        $type = $this->mHelpTypeModel->getAllType('id,title');
        $this->assign('help_type', $type);
        $list = $this->mHelpModel->getList($map,$order);
        $this->assign('list', $list);
        $this->setTitle('帮忙');
        $this->assign('filter', $post);
        $this->display();
    }

    /**
     * getMap  获取查询条件
     * @param $post
     * @return mixed
     * @author:xjw129xjt xjt@ourstu.com
     */
    protected function getMap(&$post)
    {
        foreach ($post as $k => $v) {
            if ($k == 'sex') {
                if ($v != -1) {
                    $map[$k] = $v;
                }
            }  elseif ($k == 'state') {
                if ($v != -1) {
                    $map[$k] = $v;
                }
                if($v == 7){
                   $map[$k] = array('in',array(5,6,7));
                }

            } elseif (!in_array($k, array('s_age', 's_height', 's_time', 'e_age', 'e_height', 'e_time', 's_number', 'e_number','sort'))) {
                if (!empty($v)) {
                    $map[$k] = $v;
                }
            }
        }

        unset($k,$v);
        if (!empty($post['s_number']) && !empty($post['e_number'])) {
            $map['number'] = array('between', array($post['s_number'], $post['e_number']));
        }
        if (!empty($post['s_number']) && empty($post['e_number'])) {
            $map['number'] = array('egt', $post['s_number']);
        }
        if (empty($post['s_number']) && !empty($post['e_number'])) {
            $map['number'] = array('elt', $post['e_number']);
        }


        $map_age = $this->parseMap($post, 'age');
        $map_height = $this->parseMap($post, 'height');
        $map_time = $this->parseMap($post, 'time');
        $where = '';
        $map_age && $where .= $where ? ' AND ' . $map_age : $map_age;
        $map_height && $where .= $where ? ' AND ' . $map_height : $map_height;
        $map_time && $where .= $where ? ' AND ' . $map_time : $map_time;
        $where && $map['_string'] = $where;
        $map['status'] = 1;
        return $map;
    }

    /**
     * parseMap  处理条件
     * @param $post
     * @param $key
     * @return string
     * @author:xjw129xjt xjt@ourstu.com
     */
    protected function parseMap(&$post, $key)
    {
        if ($post['s_' . $key] > $post['e_' . $key] && !empty($post['e_' . $key])) {
            $post['s_' . $key] = 0;
            $post['e_' . $key] = 0;
            return;
        }
        if (!empty($post['s_' . $key]) && !empty($post['e_' . $key])) {
            $map = '((`s_' . $key . '` <= ' . $post['e_' . $key] . ') and (`e_' . $key . '` >= ' . $post['s_' . $key] . '))';
        } elseif (!empty($post['s_' . $key]) && empty($post['e_' . $key])) {
            $map = '(`e_' . $key . '` >= ' . $post['s_' . $key] . ')';
        } elseif (empty($post['s_' . $key]) && !empty($post['e_' . $key])) {
            $map = '(`s_' . $key . '` <= ' . $post['e_' . $key] . ')';
        } elseif (empty($post['s_' . $key]) && empty($post['e_' . $key])) {
            $map = '';
        }

        return $map;
    }

    public function ask($id = 0)
    {
        if (!is_login()) {
            $this->error('请登录后再进行操作！');
        }
        if (IS_POST) {
            $data['id'] = $id = intval(I('post.help_id'));
            $data['title'] = $title = op_t(I('post.title'));
            $data['description'] = $description = op_h(I('post.description'));
            $data['province'] = $province = intval(I('post.province'));
            $data['city'] = $city = intval(I('post.city'));
            $data['district'] = $district = intval(I('post.district'));
            $data['s_time'] = $s_time = intval(strtotime(I('post.s_time')));
            $data['e_time'] = $e_time = intval(strtotime(I('post.e_time')));
            $data['type'] = $type = intval(I('post.type'));
            $data['sex'] = $sex = intval(I('post.sex'));
            $data['s_age'] = $s_age = intval(I('post.s_age'));
            $data['e_age'] = $e_age = intval(I('post.e_age'));
            $data['s_height'] = $s_height = intval(I('post.s_height'));
            $data['e_height'] = $e_height = intval(I('post.e_height'));
            $data['number'] = $number = intval(I('post.number'));
            $data['cover_id'] = $cover_id = intval(I('post.cover_id'));
            if (empty($title) || mb_strlen($title, 'utf-8') < 4 || mb_strlen($title, 'utf-8') > 20) {
                $this->error('请填写4-20字标题！');
            }
            if (empty($province) || empty($city) || empty($district)) {
                $this->error('请选择正确的地区！');
            }
            if (empty($s_time) || empty($e_time)) {
                $this->error('请填写正确的时间！');
            }
            if ($s_time > $e_time) {
                $this->error('开始时间不能大于结束时间！');
            }
            if (empty($type)) {
                $this->error('请选择类型！');
            }
            if($type<=7&&$type>0){
                if ($sex != 0 && $sex != 1&&$sex!=2) {
                    $this->error('请选择性别！');
                }
                if (empty($s_age) || empty($e_age)) {
                    $this->error('请填写正确的年龄！');
                }
                if ($s_age > $e_age) {
                    $this->error('开始年龄不能大于结束年龄！');
                }

                if (empty($number)) {
                    $this->error('请填写人数！');
                }
                if($type<=4){
                    if (empty($s_height) || empty($e_height)) {
                        $this->error('请填写正确的身高！');
                    }
                    if ($s_height > $e_height) {
                        $this->error('开始身高不能大于结束身高！');
                    }
                }else{
                    $data['s_height']=0;
                    $data['e_height']=500;
                }
            }else{
                $data['sex']=2;
                $data['s_height']=0;
                $data['e_height']=500;
                $data['s_age']=0;
                $data['e_age']=1000;
                $data['number']=1000;
            }
            if (empty($description)) {
                $this->error('请填写介绍！');
            }
            if (empty($cover_id)) {
                $this->error('请上传封面图片！');
            }
            if (!$id) {
                $user_rate=query_user(array('rate'),is_login());
                $data['user_rate']=$user_rate['rate'];
                $res = $this->mHelpModel->addHelp($data);
                if ($res) {
                    $addon_config = get_addon_config('SyncLogin');
                    if(in_array('Sina',$addon_config['type'])){
                    $server_host = "http://" . $_SERVER ['HTTP_HOST'];
                    $weibo_content = '我在'.C('WEB_SITE').'发布了帮忙任务：'.$title.'，点击查看'.$server_host.U('help/index/detail', array('id' => $res));
                    sync_send_weibo($weibo_content,'sina');
                    }
                    clean_query_user_cache(is_login(),array('user_help_5','user_help_3'));
                    $this->success('发布成功！', U('help/index/detail', array('id' => $res)));

                } else {
                    $this->error('发布失败！');
                }
            } else {
                if (!check_can_edit($id)) {
                    $this->error('没有修改权限！');
                }
                $res = $this->mHelpModel->editHelp($data);
                if ($res) {
                    clean_query_user_cache(is_login(),array('user_help_5','user_help_3'));
                    $this->success('修改成功！', U('help/index/detail', array('id' => $id)));
                } else {
                    $this->error('修改失败！');
                }
            }

        } else {
            $id = intval($id);

            if ($id) {
                if (!check_can_edit($id)) {
                    $this->error('没有修改权限！');
                }
                $help = $this->mHelpModel->getHelp($id);
                $this->assign('help', $help);
                $this->setTitle('修改帮忙');
            } else {
                $this->setTitle('添加帮忙');
            }
            $type = $this->mHelpTypeModel->getAllType('id,title');
            $this->assign('help_type', $type);
            $this->assign('uid',is_login());
            $this->display();
        }
    }

    public function detail($id)
    {
        $id = I('get.id',0,'intval');
        $help = $this->mHelpModel->getHelp($id);
        if(!$help){
            $this->error('参数错误，找不到帮忙！');
        }
        $this->assign('help', $help);
        $this->assign('id', $id);
        $this->setTitle('帮忙详情-{$help.title}');


        if (check_can_edit($id)) {
            $sign_list =  $this->mHelpAttendModel->getAllSignListByHelpId($id);
        } else {
            $sign_list =  $this->mHelpAttendModel->getShowSignListByHelpId($id);
            $sign_list = array_slice($sign_list,0,3);
            $hidden_count =  $this->mHelpAttendModel->getHiddenCountByHelpId($id);
            $this->assign('hidden_count', $hidden_count);
        }
        if (check_is_accept($id) || check_can_edit($id)) {
            $accept_list =  $this->mHelpAttendModel->getAcceptByHelpId($id);
            $this->assign('accept_list', $accept_list);
        }
        $this->assign('sign_list', $sign_list);
        $this->assign('uid',$help['uid']);
        $this->display();
    }

    /**
     * signAttend  报名参加帮忙
     * @author:xjw129xjt xjt@ourstu.com
     */
    public function signAttend()
    {
        if (IS_POST) {
            if (!is_login()) {
                $this->error('请登录后再进行操作！');
            }
            $data['help_id'] = $help_id = intval(I('post.help_id'));
            $data['phone'] = $phone = doubleval(I('post.phone'));
            $data['telephone'] = $telephone = op_t(I('post.telephone'));
            $data['qq'] = $qq = intval(I('post.qq'));
            $data['weixin'] = $weixin = op_t(I('post.weixin'));
            $data['hidden'] = $hidden = intval(I('post.hidden'));
            if (!help_is_exist($help_id)) {
                $this->error('帮忙不存在！');
            }
            $help = $this->mHelpModel->getHelp($help_id);
            if (is_login() == $help['uid']) {
                $this->error('不能参加自己发布的帮忙');
            }
            if (check_is_sign($help_id)) {
                $this->error('您已经报过名了！');
            }
            if (empty($phone) && empty($telephone)) {
                $this->error('手机与电话必须填一项');
            }
            if ($phone && !preg_match('/^(1[3|4|5|8])[0-9]{9}$/', $phone)) {
                $this->error('请正确填写手机号码');
            }
            if ($telephone && !preg_match("/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/", $telephone)) {
                $this->error('请正确填写固定号码');
            }
            if (empty($qq) || !preg_match("/^[1-9]\\d{4,10}$/", $qq)) {
                $this->error('请正确填写QQ号码');
            }
            if (empty($weixin)) {
                $this->error('请正确填写微信号码');
            }

            $res =  $this->mHelpAttendModel->signHelp($data);
            if ($res) {
                M('help')->where(array('id'=>$help_id))->setInc('sign_count');
                $this->success('报名成功！', U('help/index/detail', array('id' => $help_id)));
                $msg['to_help_id']=$help_id;
                $msg['from_uid']=is_login();
                $msg['content']='报名参加了你的帮忙';
                $msg['title']='报名帮忙';
                send_help_message($msg);
            } else {
                $this->error('报名失败！');
            }

        } else {
            $this->assign('help_id', I('get.help_id'));
            $this->display();
        }
    }

    public function stopSignAttend()
    {
            if (!is_login()) {
                $this->error('请登录后再进行操作！');
            }
            $data['help_id'] = $help_id = intval(I('get.help_id'));
            if (!help_is_exist($help_id)) {
                $this->error('帮忙不存在！');
            }
            if (!check_is_sign($help_id)) {
                $this->error('您还没报过名！');
            }
            $res =  $this->mHelpAttendModel->deleteAttend($data);
            if ($res['status']) {
                M('help')->where(array('id'=>$help_id))->setDec('sign_count');
                $this->success('取消报名成功！', U('help/index/detail', array('id' => $help_id)));
                $msg['to_help_id']=$help_id;
                $msg['from_uid']=is_login();
                $msg['content']='取消了报名你的帮忙';
                $msg['title']='取消报名帮忙';
                send_help_message($msg);
            } else {
                $this->error($res['info']);
            }
    }

    public function production()
    {
        $id = I('get.id', 0, 'intval');
        $works =  $this->mHelpWorksModel->getWorks($id);
        $this->assign('works', $works);
        $this->assign('help_id', $id);
        $attend =  $this->mHelpAttendModel->getAcceptByHelpId($id);
        $this->assign('attend', $attend);
        $help = $this->mHelpModel->getHelp($id);
        $this->assign('help', $help);
        $this->setTitle('帮忙作品:{$works.title}');
        $this->assign('uid',$help['uid']);
        $this->display();
    }

    public function publishWork()
    {
        if (IS_POST) {
            $data['help_id'] = $help_id = intval(op_t(I('post.help_id')));
            $data['cover_id'] = $cover_id = intval(op_t(I('post.cover_id')));
            $data['title'] = $title = op_t(I('post.title'));
            $data['detail'] = $detail = op_t(I('post.detail'));
            $data['video'] = $video = op_t(I('post.video'));
            $data['image'] = $image = op_t(I('post.image'));
            if (!is_login()) {
                $this->error('请登录');
            }
            if (!help_is_exist($help_id)) {
                $this->error('该帮忙不存在！');
            }
            if (!check_can_edit($help_id)) {
                $this->error('没有权限上传作品');
            }
            if (empty($title) || mb_strlen($title, 'utf-8') > 20) {
                $this->error('请填写20字以内标题');
            }
            if (empty($detail) || mb_strlen($title, 'utf-8') > 150) {
                $this->error('请填写150字以内详细说明');
            }
            if (!empty($video)&&!preg_match("/(youku.com|youtube.com|qq.com|ku6.com|sohu.com|sina.com.cn|tudou.com|yinyuetai.com)/i", $video, $hosts)) {
                $this->error('仅支持优酷、土豆、搜狐、音悦台等视频发布');
            }
            if (empty($image)) {
                $this->error('请上传图片');
            }

            $res =  $this->mHelpWorksModel->addWorks($data);
            if ($res) {
                 $this->mHelpSnapshotModel->addSnapshot($help_id,2);
                $addon_config = get_addon_config('SyncLogin');
                if(in_array('Sina',$addon_config['type'])){
                $server_host = "http://" . $_SERVER ['HTTP_HOST'];
                $weibo_content = '我在'.C('WEB_SITE').'发布了帮忙作品：'.$title.'，点击查看'.$server_host.U('help/index/production', array('id' => $help_id));
                sync_send_weibo($weibo_content,'sina');
                }
                $this->success('发布成功！', U('help/index/production', array('id' => $help_id)));
            } else {
                $this->error('发布失败！');
            }

        } else {
            $id = intval(I('get.id'));

            $work =  $this->mHelpWorksModel->getWorks($id);
            $this->assign('work',$work);
            $var['unid'] = substr(strtoupper(md5(uniqid(mt_rand(), true))), 0, 8);
            $var['fileSizeLimit'] = floor(8 * 1024) . 'KB';
            $this->assign($var);
            $this->setTitle('上传作品');
            $this->assign('help_id', $id);
            $help = $this->mHelpModel->getHelp($id);
            $this->assign('uid',$help['uid']);
            $this->display();
        }

    }

    public function getVideoInfo()
    {
        $link = op_t(I('post.url'));
        if (preg_match("/(youku.com|youtube.com|qq.com|ku6.com|sohu.com|sina.com.cn|tudou.com|yinyuetai.com)/i", $link, $hosts)) {
            $return['boolen'] = 1;
            $return['data'] = $link;
        } else {
            $return['boolen'] = 0;
            $return['message'] = '仅支持优酷、土豆网、搜狐、音悦台等视频发布';
        }
        $flashinfo =  $this->mHelpWorksModel->getFlashInfo($link, $hosts[1]);
        $return['title'] = 1;
        if (!$flashinfo['title'] || json_encode($flashinfo['title']) == 'null') {
            $flashinfo['title']="未获取标题";
        }
        $return['data'] = $flashinfo;
        exit(json_encode($return));
    }


    public function attendSupport()
    {
        if (!is_login()) {
            $this->error('请登录后再操作');
        }
        $attend_id = intval(I('post.attend_id'));
        $support = intval(I('post.support'));
        $attend =  $this->mHelpAttendModel->getAttend($attend_id);
        if ($attend['status'] == 0 || empty($attend)) {
            $this->error('找不到相关记录');
        }
        $check = $this->mHelpAttendSupportModel->where(array('attend_id' => $attend_id, 'uid' => is_login()))->select();
        if ($check) {
            $this->error('您已经执行过操作');
        }
        $data['attend_id'] = $attend_id;
        $data['uid'] = is_login();
        $data['type'] = $support;
        $data['create_time'] = time();
        $res = $this->mHelpAttendSupportModel->add($data);
        if ($res) {
             $this->mHelpAttendModel->where(array('id' => $attend_id))->setInc($support ? 'support' : 'oppose');
            $this->success('操作成功！', 'refresh');
        } else {
            $this->error('操作失败！');
        }
    }

    public function worksSupport()
    {
        if (!is_login()) {
            $this->error('请登录后再操作');
        }
        $help_id = intval(I('post.help_id'));
        $support = intval(I('post.support'));
        $work =  $this->mHelpWorksModel->getWorks($help_id);
        if ($work['status'] == 0 || empty($work)) {
            $this->error('找不到相关记录');
        }
        $check =  $this->mHelpWorksSupportModel->where(array('help_id' => $help_id, 'uid' => is_login()))->select();
        if ($check) {
            $this->error('您已经执行过操作');
        }
        $data['help_id'] = $help_id;
        $data['uid'] = is_login();
        $data['type'] = $support;
        $data['create_time'] = time();
        $res =  $this->mHelpWorksSupportModel->add($data);
        if ($res) {

             $this->mHelpWorksModel->where(array('help_id' => $help_id))->setInc($support ? 'support' : 'oppose');
            S('help_work_' . $help_id, null);
            $this->success('操作成功！', 'refresh');
        } else {
            $this->error('操作失败！');
        }
    }

    public function acceptSomeone()
    {
        $aId = I('post.id', 0, 'intval');

        $attend =  $this->mHelpAttendModel->getAttend($aId);

        if (empty($attend)) {
            $this->error('获取信息失败');
        }
        if (!is_login()) {
            $this->error('请登录后再操作');
        }
        if (!check_can_edit($attend['help_id'])) {
            $this->error('没有权限操作！');
        }
        if ($attend['status']) {
            $this->error('该用户已被接受！');
        }

        $res =  $this->mHelpAttendModel->acceptSomeone($attend['help_id'], $attend['uid']);
        if ($res) {
            $msg['to_uid']=$attend['uid'];
            $msg['to_help_id']=$attend['help_id'];
            $msg['from_uid']=is_login();
            $msg['content']='接受了您的帮忙';
            $msg['title']='接受帮忙';
            send_help_message($msg);
            $this->mHelpSnapshotModel->addSnapshot($attend['help_id'],1,$attend['uid']);
            $this->success('操作成功！', 'refresh');
        } else {
            $this->error('操作失败！');
        }
    }

    public function cancelAccept()
    {
        $aId = I('post.id', 0, 'intval');

        $attend =  $this->mHelpAttendModel->getAttend($aId);

        if (empty($attend)) {
            $this->error('获取信息失败');
        }
        if (!is_login()) {
            $this->error('请登录后再操作');
        }
        if (!check_can_edit($attend['help_id'])) {
            $this->error('没有权限操作！');
        }
        if (!$attend['status']) {
            $this->error('该用户还未被接受！');
        }

        $res =  $this->mHelpAttendModel->cancelAccept($attend['help_id'], $attend['uid']);
        if ($res) {
            $this->mHelpSnapshotModel->delSnapshot($attend['help_id'],1,$attend['uid'],'help');
            $this->success('操作成功！', 'refresh');
        } else {
            $this->error('操作失败！');
        }
    }

    public function evaluate()
    {
        $page = I('get.page',1,'intval');
        $r=10;
        $id = I('get.id', 0, 'intval');
        $help = $this->mHelpModel->getHelp($id);
        $evaluate =$this->mHelpEvaluateModel->where(array('help_id' => $id,'status'=>1))->page($page,$r)->order('create_time desc')->select();
        foreach ($evaluate as &$v) {
            $v['attaches'] = explode(',',$v['attach_ids']);
            $v['user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature'), $v['uid']);
            $v['to_user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature'), $v['to_uid']);
        }
        unset($v);
        $this->assign('evaluate', $evaluate);
        $this->assign('help_id', $id);
        $this->assign('r', $r);
        $totalCount = $this->mHelpEvaluateModel->where(array('help_id' => $id,'status'=>1))->count();
        $this->assign('totalCount', $totalCount);

       $attend =   $this->mHelpAttendModel->getAcceptByHelpId($id);

        foreach($attend as $key =>&$v){
            if(check_publisher_is_evaluate($id,$help['uid'],$v['uid'])){
                unset($attend[$key]);
            }
        }
        unset($key,$v);
        $this->assign('attend', $attend);

        $this->assign('uid',$help['uid']);
        $this->setTitle('帮忙评价');
        $this->display();
    }
    public function doEvaluate(){
        $aUser = I('post.');
        if (!is_login()) {
            $this->error('请登录后再评论');
        }
        foreach($aUser as $k=>&$v){
            if(count($aUser) ==1){
                $data['help_id'] = $aHelpId = intval($v['help_id']);
                $data['content'] = $aContent = op_h($v['content']);
                $data['attach_ids'] = $aAttach_ids =op_h($v['image']);
                $data['type'] = $aType = intval($v['type']);
                $data['punctual'] = $aPunctual = intval($v['punctual']);
                $data['trustworthy'] = $aTrustworthy = intval($v['trustworthy']);
                $data['technique'] = $aTechnique = intval($v['technique']);
                $data['attitude'] = $aAttitude = intval($v['attitude']);
                $data['to_uid'] = $aToUid =intval($v['to_uid']);
                if (empty($aHelpId)) {
                    $this->error('帮忙不存在');
                }
                if (empty($aContent)) {
                    $this->error('请填写内容');
                }
                if (empty($aType)) {
                    $this->error('请选择好中差评');
                }
                if (empty($aPunctual)) {
                    $this->error('请为守时打分');
                }
                if (empty($aTrustworthy)) {
                    $this->error('请为守信打分');
                }
                if (empty($aTechnique)) {
                    $this->error('请为技能打分');
                }
                if (empty($aAttitude)) {
                    $this->error('请为态度打分');
                }
                $help = $this->mHelpModel->getHelp($aHelpId);
                if(empty($help)){
                    $this->error('帮忙不存在');
                }
                if($aToUid){
                    if( !check_is_accept($aHelpId,$aToUid)){
                        $this->error('不能评论该用户');
                    }
                    if( check_publisher_is_evaluate($aHelpId,is_login(),$aToUid) || !check_is_my_publish($aHelpId)){
                        $this->error('不能评论该用户');
                    }
                }
                $res =$this->mHelpEvaluateModel->addEvaluate($data);
                  if ($res) {
                      if($aType ==1){
                          M('member')->where(array('uid'=>$data['to_uid']))->setInc('credibility');
                          $good_count = D('HelpEvaluate')->where(array('to_uid'=>$data['to_uid'],'type'=>1))->count();
                          $evaluate_count = D('HelpEvaluate')->where(array('to_uid'=>$data['to_uid']))->count();
                          $rate =number_format($good_count/$evaluate_count*100,1);
                          M('member')->where(array('uid'=>$data['to_uid']))->setField('rate',$rate);
                          M('Help')->where(array('uid'=>$data['to_uid']))->setField('user_rate',$rate);
                      }
                      $this->mHelpSnapshotModel->addSnapshot($aHelpId,2);
                    $this->success('评论成功！','refresh');
                } else {
                    $this->error('评论失败！');
                }

            }else{
                $flag = 0;
                if(!empty($v['punctual']) && !empty($v['trustworthy']) && !empty($v['technique']) && !empty($v['attitude']) && !empty($v['content']) ){
                    $data['help_id'] = $aHelpId = intval($v['help_id']);
                    $data['content'] = $aContent = op_h($v['content']);
                    $data['attach_ids'] = $aAttach_ids =op_h($v['image']);
                    $data['type'] = $aType = intval($v['type']);
                    $data['punctual'] = $aPunctual = intval($v['punctual']);
                    $data['trustworthy'] = $aTrustworthy = intval($v['trustworthy']);
                    $data['technique'] = $aTechnique = intval($v['technique']);
                    $data['attitude'] = $aAttitude = intval($v['attitude']);
                    $data['to_uid'] = $aToUid =intval($v['to_uid']);
                    $res =$this->mHelpEvaluateModel->addEvaluate($data);
                    if($res && $aType ==1){
                        M('member')->where(array('uid'=>$data['to_uid']))->setInc('credibility');
                        $good_count = D('HelpEvaluate')->where(array('to_uid'=>$data['to_uid'],'type'=>1))->count();
                        $evaluate_count = D('HelpEvaluate')->where(array('to_uid'=>$data['to_uid']))->count();
                        $rate =number_format($good_count/$evaluate_count*100,1);
                        M('member')->where(array('uid'=>$data['to_uid']))->setField('rate',$rate);
                        M('Help')->where(array('uid'=>$data['to_uid']))->setField('user_rate',$rate);
                    }
                    $flag = 1;
                }
            }
        }
        unset($k,$v);
        if($flag){
            $this->mHelpSnapshotModel->addSnapshot($aHelpId,2);
            $this->success('评价成功','refresh');
        } else{
            $this->error('请填写内容');
        }
    }

    public function snapshotDetail()
    {
        $id = I('get.id',0,'intval');
        $snapshot = $this->mHelpSnapshotModel->getSnapshot($id);
        if(empty($snapshot['stage'])){
            $this->error('参数错误');
        }
        $this->assign('snapshot',$snapshot);

        $this->assign('help_id',$id);

        $help = $this->mHelpModel->getHelp($id);
        $this->assign('uid',$help['uid']);

        $this->display();
/*      $snapshot = $this->mHelpSnapshotModel->addSnapshot($id,2);
        dump($snapshot);*/

    }

}